// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "order_number": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "订单编号"
  },
  "user_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "用户id"
  },
  "create_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "defaultValue": {
      "$env": "now"
    }
  },
  "job_type_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "工种类型"
  },
  "employment_time": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "label": "用工时间"
  },
  "address": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "地址"
  },
  "longitude": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "经度"
  },
  "latitude": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "纬度"
  },
  "house_number": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "门牌号"
  },
  "charging_mode": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "自己出价",
            "value": 0
          },
          {
            "text": "计时",
            "value": 1
          }
        ]
      }
    ],
    "label": "收费模式",
    "defaultValue": 0
  },
  "require_peoples": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "需要人数"
  },
  "male_numbers": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "男工人数"
  },
  "female_numbers": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "女工人数"
  },
  "require_work_time": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "需要工作时间"
  },
  "charging_unit": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "小时",
            "value": 0
          },
          {
            "text": "天",
            "value": 1
          }
        ]
      }
    ],
    "label": "计费单位",
    "defaultValue": 0
  },
  "offer": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "出价"
  },
  "supplement": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 100
      }
    ],
    "label": "补充文本"
  },
  "images": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "file"
      }
    ],
    "label": "补充图片"
  },
  "status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "进行中",
            "value": 0
          },
          {
            "text": "待支付",
            "value": 1
          },
          {
            "text": "待评价",
            "value": 2
          },
          {
            "text": "已完成",
            "value": 3
          },
          {
            "text": "已取消",
            "value": 4
          }
        ]
      }
    ],
    "defaultValue": 0
  }
}

const enumConverter = {
  "charging_mode_valuetotext": {
    "0": "自己出价",
    "1": "计时"
  },
  "charging_unit_valuetotext": {
    "0": "小时",
    "1": "天"
  },
  "status_valuetotext": {
    "0": "进行中",
    "1": "待支付",
    "2": "待评价",
    "3": "已完成",
    "4": "已取消"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
